import { stringify } from 'querystring';
import { router } from 'umi';
import {
  fakeAccountLogin,
  getFakeCaptcha,
  userLoginPost,
  updatePwdPost,
  queryMenuList,
} from '@/services/login';
import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import defaultSettings from '../../config/defaultSettings';
import store from 'store';
const Model = {
  namespace: 'login',
  state: {
    status: undefined,
    menuList: [], //菜单列表
  },
  effects: {
    *login({ payload }, { call, put }) {
      const data = yield call(userLoginPost, payload);
      // const { success, message, status, ...other } = data
      if (data.code === 0) {
        const {
          data: { accessToken },
        } = data;
        localStorage.setItem('Authorization', accessToken);
        // const { userName: username, staffId: id, telephone, org } = { "_csrf":"5251f351-0936-4116-a083-a7be4e7eca86", "telephone":"1300728888", "userName":"91110228MA001HEP2E", "org":{ "orgName":"北京自如生活企业管理有限公司", "orgId":358 }, "staffId":79806 }
        // const { userName: username, staffId: id, telephone, org } = { "_csrf":"bec0d7b5-b933-4f91-8065-7d8dd8e94d51", "telephone":"", "userName":"91110105600024399J", "org":{ "orgName":"北京中原房地产经纪有限公司", "orgId":16297 }, "staffId":79924 }
        const {
          data: { orgMap, rolesId, staffId, systemId, loginName, loginTime, authCodes },
        } = data;
        const user = { orgMap, rolesId, staffId, systemId, loginName, loginTime, authCodes };
        store.set('user', user);

        // const urlParams = new URL(window.location.href);
        // const params = getPageQuery();
        // let { redirect } = params;

        // if (redirect) {
        //   const redirectUrlParams = new URL(redirect);

        //   if (redirectUrlParams.origin === urlParams.origin) {
        //     redirect = redirect.substr(urlParams.origin.length);

        //     if (redirect.match(/^\/.*#/)) {
        //       redirect = redirect.substr(redirect.indexOf('#') + 1);
        //     }
        //   } else {
        //     window.location.href = '/';
        //     return;
        //   }
        // }

        // router.replace(redirect || '/');
        router.push('/');
      }
      return data;
    },
    // *login({ payload }, { call, put }) {
    //   const response = yield call(fakeAccountLogin, payload);
    //   yield put({
    //     type: 'changeLoginStatus',
    //     payload: response,
    //   }); // Login successfully

    //   if (response.status === 'ok') {
    //     const urlParams = new URL(window.location.href);
    //     const params = getPageQuery();
    //     let { redirect } = params;

    //     if (redirect) {
    //       const redirectUrlParams = new URL(redirect);

    //       if (redirectUrlParams.origin === urlParams.origin) {
    //         redirect = redirect.substr(urlParams.origin.length);

    //         if (redirect.match(/^\/.*#/)) {
    //           redirect = redirect.substr(redirect.indexOf('#') + 1);
    //         }
    //       } else {
    //         window.location.href = '/';
    //         return;
    //       }
    //     }

    //     router.replace(redirect || '/');
    //   }
    // },

    *getCaptcha({ payload }, { call }) {
      yield call(getFakeCaptcha, payload);
    },

    *updatePwdPost({ payload }, { call }) {
      const data = yield call(updatePwdPost, payload);
      // const { success, message, status, ...other } = data
      return data;
    },
    *queryMenuList({ payload }, { call, put }) {
      const data = yield call(queryMenuList, {systemId: defaultSettings.systemId});
      // return data
      const { success, msg, ...other } = data;
      if (success) {
        yield put({
          type: 'setState',
          payload: {
            menuList: other.data || {},
          },
        });
      } else {
        throw data;
      }
    },
    logout() {
      // const { redirect } = getPageQuery(); // Note: There may be security issues, please note
      localStorage.setItem('Authorization', '');
      store.set('user', {});
      if (window.location.pathname !== '/user/login') {
        router.replace({
          pathname: '/user/login',
          // search: stringify({
          //   redirect: window.location.href,
          // }),
        });
      }
    },
  },
  reducers: {
    setState(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoginStatus(state, { payload }) {
      setAuthority(payload.currentAuthority);
      return { ...state, status: payload.status, type: payload.type };
    },
  },
};
export default Model;
